// pages/order/details/details.js
const app = getApp();
const service = require('../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否核销
    isCheck: false,
    // 订单id
    orderSn: '',
    // 订单状态
    orderStatus: 2,
    // 是否显示开票按钮
    invoice: 0,
    // 是否开票
    isApplyInvoice: false,
    // 发票信息
    invoiceInfo: {
      // 抬头
      headName: null,
      // 类型
      type: 1,
      // 税号
      dutyNumber: null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      orderSn: options.orderSn,
      isCheck: options.isCheck || false
    })
  },

  // 加载详情
  loadData() {
    var that = this;
    service.getConnect(service.api.getOrderDetails, { orderSn: this.data.orderSn }).then(function (res) {
      console.log(res);
      that.setData(res.data);
      wx.hideLoading();
    })
  },

  // 申请开票
  bindApplyInvoice() {
    this.setData({
      isApplyInvoice: !this.data.isApplyInvoice
    })
  },

  // 发票抬头类型选择
  bindInvoiceTitleTypeChange(e) {
    // console.log(e.currentTarget.dataset.value);
    this.setData({
      ['invoiceInfo.type']: e.currentTarget.dataset.value
    })
  },

  // 税号填写
  bindInvoiceHeadNameInput(e) {
    this.setData({
      ['invoiceInfo.headName']: e.detail.value
    })
  },

  // 税号填写
  bindInvoiceDutyNumberInput(e){
    this.setData({
      ['invoiceInfo.dutyNumber']: e.detail.value
    })
  },

  // 提交开票申请
  bindSubmitInvoice(){
    var that = this;
    var params = this.data.invoiceInfo;
    params.orderSn = this.data.orderSn,
    service.getConnect(service.api.submintInvoice, params).then(function (res) {
      wx.hideLoading();
      wx.showToast({
        title: '提交成功~',
      });
      that.setData({
        invoice: 1
      })
    })
  },

  // 取消订单
  cancelOrder(){
    var that = this;
    service.getConnect(service.api.cancelOrder, { orderSn: this.data.orderSn }).then(function (res) {
      wx.hideLoading();
      wx.showToast({
        title: '取消成功~',
      })
      that.loadData();
    })
  },

  // 立即购买
  bindBuyNow(e) {
    let that = this;
    let param = {
      formId: e.detail.formId,
      orderSn: this.data.orderSn
    };
    service.getConnect(service.api.payForOrder, param).then(function (res) {
      that.doWxPay(res.data);
      wx.hideLoading();
    });
  },

  // 调起微信支付
  doWxPay(e) {
    var that = this;
    wx.requestPayment({
      timeStamp: e.timeStamp,
      nonceStr: e.nonceStr,
      package: e.package,
      signType: e.signType,
      paySign: e.paySign,
      success(res) {
        console.log(res);
        wx.showToast({
          title: '支付成功~',
          mask: true
        })
        wx.showLoading({
          title: '请稍后...',
          mask: true
        });
        that.checkPayStatus(e.orderSn);
      },
      fail(res) {
        console.log(res);
        app.showConfirm('提示', res.msg, false);
      }
    })
  },

  checkPayStatus(orderSn) {
    var that = this;
    service.getConnect(service.api.checkOrderPayStatus, { orderSn: orderSn }).then(function (res) {
      console.log(res);
      wx.hideLoading();
      if (res.data.isPay === 1) {
        // that.loadData();
        wx.redirectTo({
          url: '/pages/order/payResult/index?orderSn=' + orderSn,
        })
      } else {
        setTimeout(function () {
          that.checkPayStatus(orderSn);
        }, 1000);
      }
    })
  },

  // 联系司机
  bindToCall(){
    wx.makePhoneCall({
      phoneNumber: this.data.driverData.mobile,
    })
  },

  // 核销租车订单
  checkRentCarOrder() {
    service.getConnect(service.api.rentCarCheckQRCode, { orderSn: this.data.orderSn }).then(function (res) {
      if(res.code===0){
        wx.hideLoading();
        app.showConfirm('提示', '核销成功', false, ()=>{
          wx.navigateBack();
        });
      } else {
        app.showConfirm('提示', '核销失败');
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    that.loadData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }

  }
})
